home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / conversion / dltogl / source.lha / dltogl_orig.c next >
C/C++ Source or Header  |  1995-02-07  |  9KB  |  437 lines

  1. From alt.graphics.pixutils Thu Aug 20 10:40:57 1992
  2. Xref: a2i alt.binaries.pictures.erotica:2800 alt.graphics.pixutils:312
  3. Newsgroups: alt.binaries.pictures.erotica,alt.graphics.pixutils
  4. Path: a2i!mips!darwin.sura.net!wupost!uunet!ferkel.ucsb.edu!piggy!jim
  5. From: jim@ferkel.ucsb.edu (Jim Lick)
  6. Subject: dltogl convertor
  7. Message-ID: <jim.714248897@piggy>
  8. Organization: University of California, Santa Barbara
  9. Date: Wed, 19 Aug 1992 18:28:17 GMT
  10. Lines: 424
  11.  
  12. This is something I found while poking around my old files.  I have
  13. tried it on a few of the recently posted files, and the converted
  14. gls run fine under xviewgl.  I tested it on SunOS 4.1.1 and Esix
  15. SVR4 4.0.3a systems.
  16.  
  17.                             Jim Lick               
  18. Work: University of California    | Play: 1236 Camino Meleno
  19.       Santa Barbara        |       Santa Barbara, CA 93111-1007
  20.       Dept. of Mechanical Engr. |    (805) 964-2088 voice/msg
  21.       2311 Engr II Building     |    (805) MUD-SPY1 data
  22.       (805) 893-4113            |       jim@tcp.com
  23.       jim@ferkel.ucsb.edu    | This space intentionally left blank
  24. ----
  25.  
  26. /*
  27.  * dltogl.c -- convert .DL animations into .GL animations.
  28.  *
  29.  * usage: dltogl file.dl [file.gl]
  30.  *
  31.  * If no .gl file is specified, the images and control file will be
  32.  * written to separate files.  The control file will always be "dl.txt",
  33.  * the images "clpN.clp" and a palette file called "palette.pic".
  34.  *
  35.  * Author:
  36.  *    George Phillips
  37.  *    <phillips@cs.ubc.ca>
  38.  */
  39.  
  40. #include <stdio.h>
  41. #ifdef __TURBOC__
  42. #include <alloc.h>
  43. #else
  44. extern char* malloc();
  45. #endif
  46.  
  47. #define FOW    "wb"
  48.  
  49. FILE*    gl;
  50. int        gl_fileno;
  51.  
  52. FILE* file_open();
  53. int file_close();
  54.  
  55. #define isneg16(x)    ((x) & 0x8000)
  56. #define neg16(x)    ((~(x) + 1) & 0x7fff)
  57.  
  58. main(argc, argv)
  59. int        argc;
  60. char*    argv[];
  61. {
  62.     unsigned int i, j;
  63.     FILE* fp;
  64.     int num_scrn;
  65.     int ctl_len;
  66.     int ch;
  67.     int format;
  68.     static char title[21];
  69.     static char author[21];
  70.     static unsigned char pal[256*3];
  71.     unsigned char* screen;
  72.     static unsigned char halfscreen[160*100];
  73.     static char fname[32];
  74.     int picnum;
  75.     int images_per_screen;
  76.     FILE* ctl;
  77.     int fx, fy;
  78.     int* cmd;
  79.     int labelpos;
  80.     int cmdnum;
  81.  
  82.     if (argc != 2 && argc != 3)
  83.         die("usage: dltogl file.dl [file.gl]");
  84.  
  85.     if (!(fp = fopen(argv[1], "rb"))) {
  86.         fprintf(stderr, "dltogl: can't open %s\n", argv[1]);
  87.         exit(1);
  88.     }
  89.  
  90.     gl = 0;
  91.     gl_fileno = 0;
  92.     if (argc == 3 && !(gl = fopen(argv[2], FOW))) {
  93.         fprintf(stderr, "dltogl: can't open %s for writing\n", argv[2]);
  94.         exit(1);
  95.     }
  96.  
  97.     if (fgetc(fp) != 2)
  98.         die("dltogl: only version 2 files can be handled");
  99.  
  100.     switch (format = fgetc(fp)) {
  101.     case 0:
  102.         fx = fy = 0;
  103.         images_per_screen = 1;
  104.         break;
  105.     case 1:
  106.         fx = 80;
  107.         fy = 50;
  108.         images_per_screen = 4;
  109.         break;
  110.     default:
  111.         die("dltogl: only large and medium formats are handled");
  112.         break;
  113.     }
  114.  
  115.     title[20] = author[20] = 0;
  116.     for (i = 0; i < 20; i++)
  117.         title[i] = fgetc(fp) ^ 255;
  118.  
  119.     for (i = 0; i < 20; i++)
  120.         author[i] = fgetc(fp) ^ 255;
  121.  
  122.     for (i = 0; i < 20; i++) {
  123.         if ((unsigned char)title[i] == 255) title[i] = 0;
  124.         if ((unsigned char)author[i] == 255) author[i] = 0;
  125.     }
  126.  
  127.     num_scrn = fgetc(fp);
  128.     ctl_len = fgetc(fp);
  129.  
  130.     if (!(cmd = (int*)malloc(ctl_len * sizeof(int))))
  131.         die("dltogl: out of memory");
  132.  
  133.     gl_numfiles(num_scrn * images_per_screen + 1 + 1);
  134.  
  135.     /* mebbe this is the border colour? */
  136.     for (i = 0; i < 3; i++)
  137.         fgetc(fp);
  138.  
  139.     for (i = 0; i < 256; i++) {
  140.         pal[i*3] = fgetc(fp);
  141.         pal[i*3+1] = fgetc(fp);
  142.         pal[i*3+2] = fgetc(fp);
  143.     }
  144.  
  145.     if (!(screen = malloc(320 * 200)))
  146.         die("dltogl: not enough memory.");
  147.  
  148.     memset(screen, 0, 320 * 200);
  149.     writepic(screen, 320, 200, "palette.pic", pal);
  150.  
  151.     picnum = 0;
  152.     for (j = 0; j < num_scrn; j++) {
  153.         fread(screen, 320 * 200, 1, fp);
  154.         switch (format) {
  155.         case 0: /* large */
  156.             sprintf(fname, "clp%d.clp", picnum++);
  157.             writepic(screen, 320, 200, fname, (unsigned char*)0);
  158.             break;
  159.         case 1: /* medium */
  160.             for (i = 0; i < 4; i++) {
  161.                 unsigned char*    src;
  162.                 unsigned char*    dst;
  163.                 int                row;
  164.                 int                col;
  165.  
  166.                 sprintf(fname, "clp%d.clp", picnum++);
  167.                 src = screen + (i % 2) * 160 + (i / 2) * 100 * 320;
  168.                 dst = halfscreen;
  169.                 for (row = 0; row < 100; row++) {
  170.                     for (col = 0; col < 160; col++)
  171.                         *dst++ = *src++;
  172.                     src += 160;
  173.                 }
  174.                 writepic(halfscreen, 160, 100, fname, (unsigned char*)0);
  175.             }
  176.         }
  177.     }
  178.  
  179.     ctl = file_open("dl.txt", FOW);
  180.  
  181.     fprintf(ctl, "; This GL file was converted from DL format by dltogl\r\n");
  182.     fprintf(ctl, "; Title:  %s\r\n", title);
  183.     fprintf(ctl, "; Author: %s\r\n", author);
  184.     /* could print all the other keeno information */
  185.  
  186.     fprintf(ctl, "video l\r\n");
  187.     fprintf(ctl, "pload palette,1\r\n");
  188.     fprintf(ctl, "palette 1\r\n");
  189.     fprintf(ctl, "pfree 1\r\n");
  190.     for (i = 0; i < num_scrn * images_per_screen; i++)
  191.         fprintf(ctl, "cload clp%d,%d\r\n", i, i + 1);
  192.  
  193.     for (i = 0; i < ctl_len; i++) {
  194.         j = fgetc(fp);
  195.         j += fgetc(fp) << 8;
  196.         cmd[i] = j;
  197.     }
  198.  
  199.     labelpos = 0;
  200.     if (isneg16(cmd[ctl_len - 1])) {
  201.         labelpos = neg16(cmd[ctl_len - 1]) + 1;
  202.         ctl_len--;    /* ignore that last command */
  203.     }
  204.  
  205.     for (i = 0, cmdnum = 0; i < ctl_len; i++, cmdnum++) {
  206.         if (cmdnum == labelpos)
  207.             fprintf(ctl, "forever:\r\n");
  208.         if (isneg16(cmd[i])) {
  209.             i++;
  210.             fprintf(ctl, "waitkey %d\r\n", cmd[i]);
  211.         }
  212.         else
  213.             fprintf(ctl, "putup %d,%d,%d,10\r\n", fx, fy, cmd[i] + 1);
  214.     }
  215.     fprintf(ctl, "goto forever\r\n");
  216.     fputc(26, ctl);
  217.     file_close(ctl);
  218.  
  219.     fclose(fp);
  220.  
  221.     if (gl)
  222.         fclose(gl);
  223.  
  224.     exit(0);
  225. }
  226.  
  227. die(s)char*s;{fprintf(stderr,"%s\n",s);exit(1);}
  228.  
  229. #define BLOCKSIZE    (8192)
  230.  
  231. writepic(pixel, width, height, filename, cmap)
  232. unsigned char* pixel;
  233. int    width;
  234. int    height;
  235. char* filename;
  236. unsigned char* cmap;
  237. {
  238.     FILE*    fp;
  239.     unsigned int i;
  240.     static unsigned char buf[BLOCKSIZE];
  241.     unsigned char* p;
  242.     int row, col;
  243.  
  244.     fp = file_open(filename, FOW);
  245.  
  246.     /* write header */
  247.     writeshort(fp, 0x1234);    /* magic number */
  248.     writeshort(fp, width);
  249.     writeshort(fp, height);
  250.     writeshort(fp, 0);
  251.     writeshort(fp, 0);
  252.     fputc(8, fp);                        /* bits per pixel */
  253.     fputc(0xff, fp);                    /* byte here is always 255 */
  254.     fputc('L', fp);                        /* video mode */
  255.     if (cmap) {
  256.         writeshort(fp, 4);        /* extra information descriptor */
  257.         writeshort(fp, 768);    /* extra information length */
  258.         fwrite(cmap, 768, 1, fp);
  259.     }
  260.     else {
  261.         writeshort(fp, 0);
  262.         writeshort(fp, 0);
  263.     }
  264.  
  265.     /* number of packed blocks in file */
  266.     writeshort(fp, ((long)width * height + BLOCKSIZE - 1) / BLOCKSIZE);
  267.  
  268.     i = 0;
  269.     for (row = height - 1; row >= 0; row--) {
  270.         p = pixel + row * width;
  271.         for (col = 0; col < width; col++) {
  272.             buf[i++] = *p++;
  273.             if (i == BLOCKSIZE) {
  274.                 outpackblock(fp, buf, i);
  275.                 i = 0;
  276.             }
  277.         }
  278.     }
  279.     if (i > 0)
  280.         outpackblock(fp, buf, i);
  281.  
  282.     file_close(fp);
  283. }
  284.  
  285. outpackblock(fp, buf, len)
  286. FILE*    fp;
  287. char*    buf;
  288. int    len;
  289. {
  290.     static char    packbuf[BLOCKSIZE * 4 - 1];    /* overkill */
  291.     int    packlen;
  292.  
  293.     packlen = packblock(buf, packbuf, len);
  294.     fwrite(packbuf, packlen, 1, fp);
  295. }
  296.  
  297. packblock(src, dest, len)
  298. char*    src;
  299. char*    dest;
  300. int        len;
  301. {
  302.     unsigned char*    s;
  303.     unsigned char*    r;
  304.     unsigned char*    d;
  305.     static int    overhead[256];
  306.     int    i;
  307.     int    esc;
  308.     int    escpresent;
  309.     int    minover;
  310.     int    packlen;
  311.  
  312.     for (i = 0; i < 256; i++)
  313.         overhead[i] = 0;
  314.  
  315.     /* do first pass to find optimal esc byte */
  316.     for (s = src; s < src + len; s = r) {
  317.         for (r = s; *r == *s && r < src + len; r++)
  318.             ;
  319.         if (r - s < 4)
  320.             overhead[*s]++;
  321.     }
  322.  
  323.     minover = len + 1;
  324.     for (i = 0; i < 256; i++) {
  325.         if (overhead[i] < minover) {
  326.             minover = overhead[i];
  327.             esc = i;
  328.         }
  329.     }
  330.     escpresent = overhead[esc] == 0;
  331.  
  332.     /* now run-length encode on the second pass */
  333.  
  334.     d = dest + 5;
  335.     for (s = src; s < src + len; s = r) {
  336.         for (r = s; *r == *s && r < src + len; r++)
  337.             ;
  338.         if (r - s < 4 && (!escpresent || *s != esc)) {
  339.             while (s < r)
  340.                 *d++ = *s++;
  341.         }
  342.         else {
  343.             if (r - s > 255) {
  344.                 *d++ = esc;
  345.                 *d++ = 0;
  346.                 *d++ = (r - s) & 0xff;
  347.                 *d++ = ((r - s) >> 8) & 0xff;
  348.                 *d++ = *s++;
  349.             }
  350.             else {
  351.                 *d++ = esc;
  352.                 *d++ = r - s;
  353.                 *d++ = *s++;
  354.             }
  355.         }
  356.     }
  357.  
  358.     packlen = d - dest;
  359.  
  360.     /* fill in the block header */
  361.  
  362.     *dest++ = packlen & 0xff;            /* packed block size */
  363.     *dest++ = ((packlen) >> 8) & 0xff;
  364.     *dest++ = len & 0xff;
  365.     *dest++ = (len >> 8) & 0xff;        /* unpacked block size */
  366.     *dest++ = esc;                        /* escape byte */
  367.  
  368.     return packlen;
  369. }
  370.  
  371. writeshort(fp, s)
  372. FILE*    fp;
  373. int        s;
  374. {
  375.     fputc(s & 255, fp);
  376.     fputc((s >> 8) & 255, fp);
  377. }
  378.  
  379. writelong(fp, l)
  380. FILE*    fp;
  381. long    l;
  382. {
  383.     fputc(l & 255, fp);
  384.     fputc((l >> 8) & 255, fp);
  385.     fputc((l >> 16) & 255, fp);
  386.     fputc((l >> 24) & 255, fp);
  387. }
  388.  
  389. long gl_eof;
  390. long gl_filestart;
  391.  
  392. gl_numfiles(n)
  393. int    n;
  394. {
  395.     if (gl) {
  396.         fseek(gl, 0, 0);
  397.         writeshort(gl, n * 17);
  398.         gl_eof = 2 + n * 17;
  399.     }
  400. }
  401.  
  402. FILE* file_open(name, mode)
  403. char*    name;
  404. char*    mode;
  405. {
  406.     FILE*    fp;
  407.     char    fname[14];
  408.  
  409.     if (!gl) {
  410.         if (!(fp = fopen(name, mode))) {
  411.             fprintf(stderr, "dltogl: can't open %s\n", name);
  412.             exit(1);
  413.         }
  414.         return fp;
  415.     }
  416.  
  417.     fseek(gl, 2 + gl_fileno++ * 17, 0);
  418.     writelong(gl, gl_eof);
  419.     memset(fname, 0, 13);
  420.     strcpy(fname, name);
  421.     fwrite(fname, 13, 1, gl);
  422.     fseek(gl, gl_filestart = gl_eof + 4, 0);
  423.     return gl;
  424. }
  425.  
  426. int file_close(fp)
  427. FILE*    fp;
  428. {
  429.     if (!gl)
  430.         return fclose(fp);
  431.  
  432.     gl_eof = ftell(gl);
  433.     fseek(gl, gl_filestart - 4, 0);
  434.     writelong(gl, gl_eof - gl_filestart);
  435. }
  436.  
  437.